﻿using Furion.DependencyInjection;
using IMES.Entity;

namespace IMES.BLL
{
    /// <summary>
    /// 角色-仓储处理
    /// </summary>
    public class RoleRep : BaseRepository<Role>, ITransient
    {
        /// <summary>
        /// 所属角色
        /// </summary>
        /// <returns></returns>
        public List<Role> AllRoleList()
        {
            return Context.Queryable<Role>().Where(r=>!r.IsDeleted).ToList();
        }

        public List<RoleMenu> GetMenusByRole(long roleId)
        {
            return Context.Queryable<RoleMenu>().Where(rm=>rm.RoleID.Equals(roleId)).ToList();
        }

        public void SaveRoleMenus(long roleId, List<string> menuNos)
        {
            //1.删除原角色菜单授权
            Context.Deleteable<RoleMenu>().Where(rm => rm.RoleID.Equals(roleId)).ExecuteCommand();
            //2.添加新的角色菜单授权
            List<RoleMenu> roleMenus = new List<RoleMenu>();
            foreach(var item in menuNos)
            {
                roleMenus.Add(new RoleMenu() { RoleID = roleId, MenuNo = item });
            }
            Context.Insertable<RoleMenu>(roleMenus).ExecuteCommand();
        }
    }
}
